package virtualinit;

import virtualinit.util.JdbcUtil;
import virtualinit.util.Log;
import virtualinit.util.TimeUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ChannelStoreRelationInit extends AbstractInit {
    static Log log = Log.instance;

    /**
     * b2c不用动用原来的channel_store_rel表数据即可, 他是固定关联的
     * <p>
     * o2o 将  channel_store_rel 清空，将store表的o2o店铺一对5（ws, jddj, jdsds, tmsds, yd五个渠道） 放到 channel_store_rel
     * <p>
     * o2o门店没有指定渠道 比如: PJ兴隆台万达, 他可以是任何o2o渠道,反正是o2o 加盟即可
     */
    public void doStart() throws Exception {
        init();
        log.info("\n\n\nchannel_store_rel 表初始化开始....");
        long start1 = System.currentTimeMillis();
        long start = System.currentTimeMillis();
        String now = TimeUtil.now();
        String scheduleDbName = JdbcUtil.quoteIdentifier(super.scheduleDbName);
        String channelStoreRelTbName = "?.sc_channel_store_rel".replace("?", scheduleDbName);


        // 删除 channel_store_rel o2o， mode_type 1->b2c , 2->o2o',
        String sql = "delete from ? where mode_type=2".replace("?", channelStoreRelTbName);
        int update = JdbcUtil.update(scheduleConn, sql);
        log.info("删除channel_store_rel中o2o数据，共：{}条 {}, 剩余b2c：{}条", update, TimeUtil.countTimeCn(start),
                JdbcUtil.count(scheduleConn, "select count(1) from ?".replace("?", channelStoreRelTbName)));


        start = System.currentTimeMillis();
        // 查询所有 o2o store信息  type=0 实体店就是o2o  status:0关店 1:开店 2未知
        List<Map<String, Object>> stores = JdbcUtil.executeToMap(scheduleConn, "select name,store_id,type,status from ?.sc_store where type=0".replace("?", scheduleDbName));
        log.info("查询store表所有o2o信息，共：{}条 {}", stores.size(), TimeUtil.countTimeCn(start));


        // 将sc_store表所有 o2o 的数据 一对五（ws, jddj, jdsds, tmsds, yd五个渠道），放入channel_store_rel 表
        String[] channels = {"WS", "JDDJ", "JDSDS", "TMSDS", "YD", "MRN", "GIFT", "PZ"};
        log.info("需要导入的channels:{}", Arrays.toString(channels));


        // store_status 0->关店，1->正常 mode_type:1->b2c , 2->o2o

        String insertSql = "insert into ? (created_time,updated_time,channel_code,store_id,channel_store_code,channel_store_name,store_status,mode_type)values"
                .replace("?", channelStoreRelTbName);
        StringBuilder sb = new StringBuilder(100000);
        sb.append(insertSql);
        int count = 0;
        int m = 0;
        for (String channelCode : channels) {
            for (Map<String, Object> store : stores) {
                Object store_id = store.get("store_id");
                super.recordRepeat(store_id, channelCode);
                sb.append(values(m++ != 0, now, now, channelCode, store_id, store_id, store.get("name"), store.get("status"), 2));
                if (++count % 1000 == 0) {
                    JdbcUtil.update(scheduleConn, sb.toString());
                    sb.setLength(insertSql.length());
                    m = 0;
                }
            }
        }

        if (sb.length() > insertSql.length()) {
            JdbcUtil.update(scheduleConn, sb.toString());
        }
        log.info("初始化channel_store_rel数据完成,o2o插入:{}条,{}", count,
                TimeUtil.countTimeCn(start1));
    }

    @Override
    public String getTaskName() {
        return "channel_store_rel同步";
    }
}
